<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="login-container"
    >
      <h3>东软网易严选商品管理系统</h3>
      <el-form-item class="login_formitem" prop="username">
        <i class="input_icon el-icon-s-custom"></i>
        <el-input
          type="text"
          v-model="ruleForm.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item class="login_formitem" prop="pwd">
        <i class="input_icon el-icon-lock"></i>
        <el-input
          type="password"
          v-model="ruleForm.pwd"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          style="width: 100%"
          type="primary"
          @click="loginFn('ruleForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/request/api";
import { SETTOKEN, GETUSERINFO } from "@/store/mutation-types";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    loginFn() {
      this.$refs["ruleForm"].validate((vali) => {
        if (vali) {
          login({
            password: this.ruleForm.pwd,
            username: this.ruleForm.username,
          }).then((res) => {
            if (res.code === 200) {
              // 存储token
              this.$store.commit(SETTOKEN, res.data.tokenHead + res.data.token);
              this.getUserInfo();
            }
          });
        }
      });
    },
    getUserInfo() {
      this.$store.dispatch(GETUSERINFO).then(res => {
        this.$router.push('/homepage')
      })
    }
  },
};
</script>

<style lang="less" scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  .login-container {
    width: 420px;
    height: 250px;
    h3 {
      padding: 10px 0;
      font-size: 24px;
      color: #fff;
      text-align: center;
    }
    .login_formitem {
      border: 1px solid hsla(0, 0, 100%, 0.1);
      background-color: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      height: 50px;

      .input_icon {
        margin-left: 10px;
        font-size: 20px;
        color: #454545;
        vertical-align: middle;
      }
      /deep/ .el-input {
        display: inline-block;
        width: 85%;
        input {
          border: 0;
          background: transparent;
          color: #fff;
          height: 50px;
        }
      }
    }
  }
}
</style>